// membitmap.c 
//	Routines to manage a bitmap -- an array of bits each of which
//	can be either on or off.  Represented as an array of integers.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"
#include "membitmap.h"
//MemBitMap *MemPageTable=new MemBitMap(32);

 MemBitMap::MemBitMap(int nitems)
{
 num=nitems;
map=new MemBit[nitems];
 int i=0;
 for(i=0;i<num;i++)
{
       map[i].isempty=1;
       map[i].counter=0;
      map[i].time=0;
       map[i].owner=NULL;
}
}

 MemBitMap::~MemBitMap()
{
 //delete map;
}

void MemBitMap::Set(int which)   //设置页框占用
 {
     ASSERT(which >= 0 && which < num);
   map[which].isempty=0;
 }

 void MemBitMap::Clear(int which)    //设置允许占用
 {
     ASSERT(which >= 0 && which < num);
  map[which].isempty=1;
 }

bool MemBitMap::Test(int which)     //该页框占用情况,返回true表示被占用
{
     ASSERT(which >= 0 && which < num);
   if(map[which].isempty==1)
      return false;
   else
      return true;
}

 int MemBitMap::Find()
{
   int i=0;
   for(i=0;i<num;i++)
   {
       if(Test(i)==false)
           return i;
   }
   return -1;
 }

int MemBitMap::NumClear()
{
   int i=0;
  int n=0;  
for(i=0;i<num;i++)
  {
       if(Test(i)==false)
           n++;
 }
  return n;
 }

 void MemBitMap::SetCounter(int which,int n)
{
     ASSERT(which >= 0 && which < num);
 if(n>=0)
       map[which].counter=n;
}

 int MemBitMap::GetCounter(int which)
{
     ASSERT(which >= 0 && which < num);
 return map[which].counter;
}

 void MemBitMap::SetTime(int which,int t)
{
     ASSERT(which >= 0 && which < num);
 if(t>=0)
      map[which].time=t;
}

 int MemBitMap::GetTime(int which)
{    ASSERT(which >= 0 && which < num);
return map[which].time;
}

void MemBitMap::SetOwner(int which,int* own)
{
     ASSERT(which >= 0 && which < num);
 map[which].owner=own;
}

 int* MemBitMap::GetOwner(int which)
{
     ASSERT(which >= 0 && which < num);
 return map[which].owner;
}


